//
//  CreateAccountView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit
import MXCommonModule

protocol CreateAccountDelegate: NSObjectProtocol {
    func createAccount()
}

class CreateAccountView: UIView {
    weak var delegate: CreateAccountDelegate?
    private var emailText: UITextField!
    private var passwordText: UITextField!
    private var passwordSureText: UITextField!
    
    var email: String{
        guard let text = self.emailText.text else { return "" }
        return text
    }
    
    var password: String{
        guard let text = self.passwordText.text else { return "" }
        return text
    }
    var passwordSure: String{
        guard let text = self.passwordSureText.text else { return "" }
        return text
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CreateAccountView{
    @objc private func createAction(){
        self.delegate?.createAccount()
    }
}

extension CreateAccountView{
    private func createXglView(){
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
       
        let contentView = UIView()
       
        let iconView = LoginUntil.creatLoginIconView()
        self.emailText = UITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let emailView = LoginUntil.creatLoginInputView(title: "Email", inputText: self.emailText)

        self.passwordText = UITextField().then({ make in
            make.textColor = UIColor.hexColor("#FF562E")
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let passwordView = LoginUntil.creatLoginInputView(title: "Password", inputText: passwordText)
        
        self.passwordSureText = UITextField().then({ make in
            make.textColor = UIColor.hexColor("#FF562E")
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let passwordSureView = LoginUntil.creatLoginInputView(title: "Confirm Password", inputText: passwordSureText)
        
        let createButton = UIButton().then { make in
            make.setTitle("Sign up", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.addTarget(self, action: #selector(self.createAction), for: .touchUpInside)
        }
        self.addSubview(createButton)
        self.addSubview(scrollView) 
        scrollView.addSubview(contentView)
        
        contentView.addSubviews([iconView,emailView,passwordView,passwordSureView])
        
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.width.equalTo(scrollView)
        }
        scrollView.snp.makeConstraints { (make) in
            make.width.centerX.equalToSuperview()
            make.top.equalTo(PanNavibarH)
            make.bottom.equalTo(createButton.snp.top).offset(-10.0)
        }
        
        iconView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 140.0 - PanNavibarH))
            make.width.height.equalTo(conversionWithScrennWidth(value: 100.0))
        }
        emailView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(iconView.snp.bottom).offset(conversionWithScrennWidth(value: 60.0))
            make.centerX.equalToSuperview()
        }
        passwordView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.top.equalTo(emailView.snp.bottom).offset(conversionWithScrennWidth(value: 32.0))
        }
        
        passwordSureView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.top.equalTo(passwordView.snp.bottom).offset(conversionWithScrennWidth(value: 32.0))
        }
        
        contentView.snp.makeConstraints { make in
            make.bottom.equalTo(passwordSureView.snp.bottom).offset(20.0)
        }
        
        createButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.bottom.equalTo(-BottomHeight-48.0)
        }
        
    }
}
